<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="线程池介绍   增减时机      keepAlive 和线程工厂   线程池用法  线程池特点 停止线程池   任务前后执行（可暂停的线程池）12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667pub">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/2021/08/21/java%E5%B9%B6%E5%8F%91%E8%BF%9B%E9%98%B6%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="线程池介绍   增减时机      keepAlive 和线程工厂   线程池用法  线程池特点 停止线程池   任务前后执行（可暂停的线程池）12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667pub">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144505.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144534.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144613.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170536.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170555.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170622.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808172559.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808172654.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173052.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173241.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173430.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173621.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808184600.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808184849.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808185326.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808215501.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808215528.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808220125.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809093352.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809095730.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809100347.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809100724.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809103220.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809104806.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809103335.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809104848.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809114410.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809114445.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809150331.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153450.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153508.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153631.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154824.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154149.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154342.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155338.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155602.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155656.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160058.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160702.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160916.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809161046.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809161929.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809162224.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809162512.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809180026.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809180113.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809184652.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809184732.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809192003.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809192055.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203401.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203514.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203636.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809205507.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210235.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210754.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210935.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809213642.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809214123.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809215201.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145155.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145224.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145240.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145505.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145606.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145657.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810153334.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810162002.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810162110.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810172334.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810172540.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810173640.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810173703.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810210844.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080047.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080313.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080754.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811095534.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102049.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102450.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102535.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811105059.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811105513.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811110357.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811110504.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811121813.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811123758.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811131712.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811131828.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811132850.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811132905.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811140642.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811165856.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811165923.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811170040.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811175340.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811184510.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811184752.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811190649.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812094029.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812102135.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812102225.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105413.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105540.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105713.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812112402.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812121712.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812123022.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812132944.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133102.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133221.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133331.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133506.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133546.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812135215.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812191545.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812192216.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812193225.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812194037.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812194948.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812212311.png">
<meta property="og:image" content="c:/Users/sunlei/AppData/Roaming/Typora/typora-user-images/image-20210812212233711.png">
<meta property="og:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812212535.png">
<meta property="article:published_time" content="2021-08-21T08:02:17.770Z">
<meta property="article:modified_time" content="2021-08-12T13:36:59.550Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144505.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-java并发进阶笔记" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/08/21/java%E5%B9%B6%E5%8F%91%E8%BF%9B%E9%98%B6%E7%AC%94%E8%AE%B0/" class="article-date">
  <time class="dt-published" datetime="2021-08-21T08:02:17.770Z" itemprop="datePublished">2021-08-21</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="线程池"><a href="#线程池" class="headerlink" title="线程池"></a>线程池</h2><h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144505.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144534.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808144613.png"></p>
<h3 id="增减时机"><a href="#增减时机" class="headerlink" title="增减时机"></a>增减时机</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170536.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170555.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808170622.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808172559.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808172654.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173052.png"></p>
<h3 id="keepAlive-和线程工厂"><a href="#keepAlive-和线程工厂" class="headerlink" title="keepAlive 和线程工厂"></a>keepAlive 和线程工厂</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173241.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173430.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808173621.png"></p>
<h3 id="线程池用法"><a href="#线程池用法" class="headerlink" title="线程池用法"></a>线程池用法</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808184600.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808184849.png"></p>
<h3 id="线程池特点"><a href="#线程池特点" class="headerlink" title="线程池特点"></a>线程池特点</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808185326.png"></p>
<h3 id="停止线程池"><a href="#停止线程池" class="headerlink" title="停止线程池"></a>停止线程池</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808215501.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808215528.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210808220125.png"></p>
<h3 id="任务前后执行（可暂停的线程池）"><a href="#任务前后执行（可暂停的线程池）" class="headerlink" title="任务前后执行（可暂停的线程池）"></a>任务前后执行（可暂停的线程池）</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PausePool</span> <span class="keyword">extends</span> <span class="title">ThreadPoolExecutor</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PausePool</span><span class="params">(<span class="keyword">int</span> corePoolSize, <span class="keyword">int</span> maximumPoolSize, <span class="keyword">long</span> keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">beforeExecute</span><span class="params">(Thread t, Runnable r)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.beforeExecute(t, r);</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> (isPaused)&#123;</span><br><span class="line">                unpaused.await();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> isPaused;</span><br><span class="line">    ReentrantLock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line">    <span class="keyword">private</span> Condition unpaused = lock.newCondition();</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">pause</span><span class="params">()</span></span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            isPaused = <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">resume</span><span class="params">()</span></span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            isPaused = <span class="keyword">false</span>;</span><br><span class="line">            unpaused.signalAll();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        PausePool pausePool = <span class="keyword">new</span> PausePool(<span class="number">10</span>,<span class="number">20</span>,<span class="number">101</span>,TimeUnit.SECONDS,</span><br><span class="line">                <span class="keyword">new</span> LinkedBlockingDeque&lt;&gt;());</span><br><span class="line">        Runnable runnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;执行&quot;</span>);</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    Thread.sleep(<span class="number">10</span>);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000</span>; i++) &#123;</span><br><span class="line">            pausePool.execute(runnable);</span><br><span class="line">        &#125;</span><br><span class="line">        Thread.sleep(<span class="number">1500</span>);</span><br><span class="line">        pausePool.pause();</span><br><span class="line">        System.out.println(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">        Thread.sleep(<span class="number">1500</span>);</span><br><span class="line">        pausePool.resume();</span><br><span class="line">        System.out.println(<span class="string">&quot;start&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="线程池原理"><a href="#线程池原理" class="headerlink" title="线程池原理"></a>线程池原理</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809093352.png"></p>
<p>线程池任务复用：相同线程执行不同任务</p>
<h3 id="线程池状态"><a href="#线程池状态" class="headerlink" title="线程池状态"></a>线程池状态</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809095730.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809100347.png"></p>
<h2 id="ThreadLocal"><a href="#ThreadLocal" class="headerlink" title="ThreadLocal"></a>ThreadLocal</h2><h3 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809100724.png"></p>
<h3 id="场景1"><a href="#场景1" class="headerlink" title="场景1"></a>场景1</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809103220.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809104806.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809103335.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 描述：     利用ThreadLocal，给每个线程分配自己的dateFormat对象，保证了线程安全，高效利用内存</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreadLocalNormalUsage05</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> ExecutorService threadPool = Executors.newFixedThreadPool(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> finalI = i;</span><br><span class="line">            threadPool.submit(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    String date = <span class="keyword">new</span> ThreadLocalNormalUsage05().date(finalI);</span><br><span class="line">                    System.out.println(date);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        threadPool.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">date</span><span class="params">(<span class="keyword">int</span> seconds)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//参数的单位是毫秒，从1970.1.1 00:00:00 GMT计时</span></span><br><span class="line">        Date date = <span class="keyword">new</span> Date(<span class="number">1000</span> * seconds);</span><br><span class="line"><span class="comment">//        SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);</span></span><br><span class="line">        SimpleDateFormat dateFormat = ThreadSafeFormatter.dateFormatThreadLocal.get();</span><br><span class="line">        <span class="keyword">return</span> dateFormat.format(date);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ThreadSafeFormatter</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> ThreadLocal&lt;SimpleDateFormat&gt;</span><br><span class="line">    dateFormatThreadLocal = <span class="keyword">new</span> ThreadLocal&lt;SimpleDateFormat&gt;()&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">protected</span> SimpleDateFormat <span class="title">initialValue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy-MM-dd hh:mm:ss&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="场景2"><a href="#场景2" class="headerlink" title="场景2"></a>场景2</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809104848.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809114410.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809114445.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809150331.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreadTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Service1().process();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Service1</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">()</span></span>&#123;</span><br><span class="line">        User user = <span class="keyword">new</span> User(<span class="string">&quot;test&quot;</span>);</span><br><span class="line">        UserHolder.holder.set(user);</span><br><span class="line">        <span class="keyword">new</span> Service2().process();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Service2</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(UserHolder.holder.get());</span><br><span class="line">        <span class="keyword">new</span> Service3().process();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Service3</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(UserHolder.holder.get());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span>  <span class="title">UserHolder</span></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> ThreadLocal&lt;User&gt; holder = <span class="keyword">new</span> ThreadLocal&lt;&gt;();</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span></span>&#123;</span><br><span class="line">    String  name;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;User&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>两个作用：</p>
<p>1、让需要用到的对象在线程捡隔离，每个线程都有自己的独立的对象</p>
<p>2、在任何方法中都可以轻松获取到该对象</p>
<p>根据对象生成实际不同选择不同而方法</p>
<p>1、initialvalue</p>
<p>在Threadlocal第一次get的时候把对象初始化出来，对象的初始化时机由我们控制</p>
<p>2、set</p>
<p>如果需要保存到threadlocal里的对象的生成时机不被我们控制，用set放到threadlocal中去。</p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153450.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153508.png"></p>
<h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809153631.png"></p>
<h3 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154824.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154149.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809154342.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155338.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155602.png"></p>
<h3 id="ThreadLocalMap"><a href="#ThreadLocalMap" class="headerlink" title="ThreadLocalMap"></a>ThreadLocalMap</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809155656.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160058.png"></p>
<h3 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h3><h4 id="内存泄漏"><a href="#内存泄漏" class="headerlink" title="内存泄漏"></a>内存泄漏</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160702.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809160916.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809161046.png"></p>
<h4 id="空指针"><a href="#空指针" class="headerlink" title="空指针"></a>空指针</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809161929.png"></p>
<p>直接返回基本类型long，会因为拆箱装箱将Null转化为Long空指针异常</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreadPoolNPE</span> </span>&#123;</span><br><span class="line">   ThreadLocal&lt;Long&gt; threadLocal =  <span class="keyword">new</span> ThreadLocal&lt;Long&gt;();</span><br><span class="line">   <span class="function"><span class="keyword">void</span> <span class="title">set</span><span class="params">()</span></span>&#123;</span><br><span class="line">       threadLocal.set(Thread.currentThread().getId());</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line">    </span><br><span class="line">   <span class="function">Long <span class="title">get</span><span class="params">()</span></span>&#123;</span><br><span class="line">       <span class="keyword">return</span> threadLocal.get();</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ThreadPoolNPE threadPoolNPE = <span class="keyword">new</span> ThreadPoolNPE();</span><br><span class="line">        System.out.println(threadPoolNPE.get());</span><br><span class="line"><span class="comment">//        new Thread(new Runnable() &#123;</span></span><br><span class="line"><span class="comment">//            @Override</span></span><br><span class="line"><span class="comment">//            public void run() &#123;</span></span><br><span class="line"><span class="comment">//                threadPoolNPE.set();</span></span><br><span class="line"><span class="comment">//                System.out.println(threadPoolNPE.get());</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;).start();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id=""><a href="#" class="headerlink" title=""></a><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809162224.png"></h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809162512.png"></p>
<h2 id="锁"><a href="#锁" class="headerlink" title="锁"></a>锁</h2><h3 id="lock"><a href="#lock" class="headerlink" title="lock"></a>lock</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809180026.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809180113.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809184652.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809184732.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809192003.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809192055.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203401.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LockInterruptibly</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Lock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    LockInterruptibly lockInterruptibly = <span class="keyword">new</span> LockInterruptibly();</span><br><span class="line">    Thread thread0 = <span class="keyword">new</span> Thread(lockInterruptibly);</span><br><span class="line">    Thread thread1 = <span class="keyword">new</span> Thread(lockInterruptibly);</span><br><span class="line">    thread0.start();</span><br><span class="line">    thread1.start();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    thread1.interrupt();</span><br><span class="line">&#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(Thread.currentThread().getName() + <span class="string">&quot;尝试获取锁&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            lock.lockInterruptibly();</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                System.out.println(Thread.currentThread().getName() + <span class="string">&quot;获取到了锁&quot;</span>);</span><br><span class="line">                Thread.sleep(<span class="number">5000</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                System.out.println(Thread.currentThread().getName() + <span class="string">&quot;睡眠期间被中断了&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                lock.unlock();</span><br><span class="line">                System.out.println(Thread.currentThread().getName() + <span class="string">&quot;释放了锁&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;获得锁期间被中断了&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="可见性"><a href="#可见性" class="headerlink" title="可见性"></a>可见性</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203514.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809203636.png"></p>
<h3 id="锁分类"><a href="#锁分类" class="headerlink" title="锁分类"></a>锁分类</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809205507.png"></p>
<h4 id="乐观锁和悲观锁"><a href="#乐观锁和悲观锁" class="headerlink" title="乐观锁和悲观锁"></a>乐观锁和悲观锁</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210235.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210754.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809210935.png"></p>
<h4 id="重入锁"><a href="#重入锁" class="headerlink" title="重入锁"></a>重入锁</h4><p>例子：预定电影院座位</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SeatLock</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> ReentrantLock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">bookSeat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;开始预定&quot;</span>);</span><br><span class="line">            Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;预定完成&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(()-&gt;bookSeat()).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(()-&gt;bookSeat()).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(()-&gt;bookSeat()).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="性质源码"><a href="#性质源码" class="headerlink" title="性质源码"></a>性质源码</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ReentrantLockTest</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> ReentrantLock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">access</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;处理&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span> (lock.getHoldCount() &lt; <span class="number">5</span>)&#123;</span><br><span class="line">                System.out.println(lock.getHoldCount());</span><br><span class="line">                access();</span><br><span class="line">                System.out.println(lock.getHoldCount());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        access();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809213642.png"></p>
<h4 id="公平锁"><a href="#公平锁" class="headerlink" title="公平锁"></a>公平锁</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809214123.png"></p>
<p>避免唤醒带来的空档期</p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210809215201.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 描述：     演示公平和不公平两种情况</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FairLock</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        PrintQueue printQueue = <span class="keyword">new</span> PrintQueue();</span><br><span class="line">        Thread thread[] = <span class="keyword">new</span> Thread[<span class="number">10</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            thread[i] = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Job(printQueue));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            thread[i].start();</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="number">100</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Job</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    PrintQueue printQueue;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Job</span><span class="params">(PrintQueue printQueue)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.printQueue = printQueue;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(Thread.currentThread().getName() + <span class="string">&quot;开始打印&quot;</span>);</span><br><span class="line">        printQueue.printJob(<span class="keyword">new</span> Object());</span><br><span class="line">        System.out.println(Thread.currentThread().getName() + <span class="string">&quot;打印完毕&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PrintQueue</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Lock queueLock = <span class="keyword">new</span> ReentrantLock(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printJob</span><span class="params">(Object document)</span> </span>&#123;</span><br><span class="line">        queueLock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">int</span> duration = <span class="keyword">new</span> Random().nextInt(<span class="number">10</span>) + <span class="number">1</span>;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;第一次正在打印，需要&quot;</span> + duration);</span><br><span class="line">            Thread.sleep(duration * <span class="number">1000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            queueLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        queueLock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">int</span> duration = <span class="keyword">new</span> Random().nextInt(<span class="number">10</span>) + <span class="number">1</span>;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;第二次正在打印，需要&quot;</span> + duration+<span class="string">&quot;秒&quot;</span>);</span><br><span class="line">            Thread.sleep(duration * <span class="number">1000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            queueLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145155.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145224.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145240.png"></p>
<h4 id="共享锁和排他锁"><a href="#共享锁和排他锁" class="headerlink" title="共享锁和排他锁"></a>共享锁和排他锁</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145505.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145606.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810145657.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">ReadWriteLock</span> </span>&#123;</span><br><span class="line">    private <span class="keyword">static</span> ReentrantReadWriteLock reentrantReadWriteLock</span><br><span class="line">             = <span class="keyword">new</span> ReentrantReadWriteLock();</span><br><span class="line">    private <span class="keyword">static</span> ReentrantReadWriteLock.ReadLock readLock</span><br><span class="line">     = reentrantReadWriteLock.readLock();</span><br><span class="line">    private <span class="keyword">static</span> ReentrantReadWriteLock.WriteLock writeLock</span><br><span class="line">            = reentrantReadWriteLock.writeLock();</span><br><span class="line">    private <span class="keyword">static</span> <span class="keyword">void</span> <span class="function"><span class="title">read</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        readLock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;读&quot;</span>);</span><br><span class="line">            Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;释放读&quot;</span>);</span><br><span class="line">            readLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    private <span class="keyword">static</span> <span class="keyword">void</span> <span class="function"><span class="title">write</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        writeLock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;写&quot;</span>);</span><br><span class="line">            Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">&quot;释放写&quot;</span>);</span><br><span class="line">            writeLock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="keyword">void</span> <span class="function"><span class="title">main</span>(<span class="params"><span class="built_in">String</span>[] args</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(ReadWriteLock::read, <span class="string">&quot;t1&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(ReadWriteLock::read, <span class="string">&quot;t2&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(ReadWriteLock::write, <span class="string">&quot;t3&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(ReadWriteLock::write, <span class="string">&quot;t4&quot;</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810153334.png"></p>
<p>总结：只能降级，不能升级，适合读多写少的场合</p>
<h4 id="自旋和阻塞"><a href="#自旋和阻塞" class="headerlink" title="自旋和阻塞"></a>自旋和阻塞</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810162002.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810162110.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810172334.png"></p>
<h4 id="可中断锁"><a href="#可中断锁" class="headerlink" title="可中断锁"></a>可中断锁</h4><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810172540.png"></p>
<h2 id="原子类"><a href="#原子类" class="headerlink" title="原子类"></a>原子类</h2><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810173640.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810173703.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AtomicInteger1</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> AtomicInteger ato</span><br><span class="line">             = <span class="keyword">new</span> AtomicInteger();</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> AtomicInteger ato1</span><br><span class="line">            = <span class="keyword">new</span> AtomicInteger();</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">incre</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ato.getAndIncrement();</span><br><span class="line">        ato1.getAndAdd(<span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keyword">int</span> basicCount = <span class="number">0</span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">increBasic</span><span class="params">()</span></span>&#123;</span><br><span class="line">        basicCount++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000</span>; i++) &#123;</span><br><span class="line">            incre();</span><br><span class="line">            increBasic();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        AtomicInteger1 r = <span class="keyword">new</span> AtomicInteger1();</span><br><span class="line">        Thread thread = <span class="keyword">new</span> Thread(r);</span><br><span class="line">        Thread thread1 = <span class="keyword">new</span> Thread(r);</span><br><span class="line">        thread.start();</span><br><span class="line">        thread1.start();</span><br><span class="line">        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        System.out.println(ato.get());</span><br><span class="line">        System.out.println(ato1.get());</span><br><span class="line">        System.out.println(basicCount);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="数组和引用"><a href="#数组和引用" class="headerlink" title="数组和引用"></a>数组和引用</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dece</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> AtomicIntegerArray array;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Dece</span><span class="params">(AtomicIntegerArray array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.array = array;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length(); i++) &#123;</span><br><span class="line">            array.getAndDecrement(i);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ince</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> AtomicIntegerArray array;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Ince</span><span class="params">(AtomicIntegerArray array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.array = array;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; array.length(); i++) &#123;</span></span><br><span class="line"><span class="comment">//            array.getAndIncrement(i);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AtomicInteger2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        AtomicIntegerArray array = <span class="keyword">new</span> AtomicIntegerArray(<span class="number">1000</span>);</span><br><span class="line">        Ince ince = <span class="keyword">new</span> Ince(array);</span><br><span class="line">        Dece dece = <span class="keyword">new</span> Dece(array);</span><br><span class="line">        Thread[] threads2 = <span class="keyword">new</span> Thread[<span class="number">10</span>];</span><br><span class="line">        Thread[] threads1 = <span class="keyword">new</span> Thread[<span class="number">10</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            threads1[i] = <span class="keyword">new</span> Thread(dece);</span><br><span class="line">            threads2[i] = <span class="keyword">new</span> Thread(ince);</span><br><span class="line">            threads1[i].start();</span><br><span class="line">            threads2[i].start();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line"></span><br><span class="line">            threads1[i].join();</span><br><span class="line">            threads2[i].join();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length(); i++) &#123;</span><br><span class="line"><span class="comment">//            if (array.get(i) != 0)&#123;</span></span><br><span class="line"><span class="comment">//                System.out.println(&quot;error&quot;+i);</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line">            System.out.println(array.get(i));</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;success&quot;</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210810210844.png"></p>
<p>升级原子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AtomiciUpdate</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> Candidate tom;</span><br><span class="line">    <span class="keyword">static</span> Candidate peter;</span><br><span class="line">    <span class="keyword">static</span> AtomicIntegerFieldUpdater&lt;Candidate&gt; sco</span><br><span class="line">             = AtomicIntegerFieldUpdater.newUpdater(Candidate.class, <span class="string">&quot;score&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000</span>; i++) &#123;</span><br><span class="line">            peter.score++;</span><br><span class="line">            sco.getAndIncrement(tom);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Candidate</span></span>&#123;</span><br><span class="line">        <span class="keyword">volatile</span> <span class="keyword">int</span> score;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        tom = <span class="keyword">new</span> Candidate();</span><br><span class="line">        peter = <span class="keyword">new</span> Candidate();</span><br><span class="line">        AtomiciUpdate atomiciUpdate = <span class="keyword">new</span> AtomiciUpdate();</span><br><span class="line">        <span class="keyword">new</span> Thread(atomiciUpdate).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(atomiciUpdate).start();</span><br><span class="line">        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        System.out.println(tom.score);</span><br><span class="line">        System.out.println(peter.score);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Adder累加器"><a href="#Adder累加器" class="headerlink" title="Adder累加器"></a>Adder累加器</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080047.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080313.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811080754.png"></p>
<h2 id="CAS"><a href="#CAS" class="headerlink" title="CAS"></a>CAS</h2><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811095534.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CASTest</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> value;</span><br><span class="line">    <span class="function"><span class="keyword">synchronized</span> <span class="keyword">int</span> <span class="title">compareAndSwap</span><span class="params">(<span class="keyword">int</span> expect, <span class="keyword">int</span> newValue)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> old = value;</span><br><span class="line">        <span class="keyword">if</span> (old == expect)&#123;</span><br><span class="line">            value = newValue;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> old;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        CASTest r = <span class="keyword">new</span> CASTest();</span><br><span class="line">        r.value = <span class="number">0</span>;</span><br><span class="line">        Thread t1 = <span class="keyword">new</span> Thread(r);</span><br><span class="line">        Thread t2 = <span class="keyword">new</span> Thread(r);</span><br><span class="line">        t1.start();</span><br><span class="line">        t2.start();</span><br><span class="line">        t1.join();</span><br><span class="line">        t2.join();</span><br><span class="line">        System.out.println(r.value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        compareAndSwap(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102049.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102450.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811102535.png"></p>
<h2 id="不变性"><a href="#不变性" class="headerlink" title="不变性"></a>不变性</h2><p>final</p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811105059.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811105513.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811110357.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811110504.png"></p>
<h2 id="并发容器"><a href="#并发容器" class="headerlink" title="并发容器"></a>并发容器</h2><p>map</p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811121813.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811123758.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811131712.png"></p>
<h3 id="concurrenthashmap"><a href="#concurrenthashmap" class="headerlink" title="concurrenthashmap"></a>concurrenthashmap</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811131828.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811132850.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811132905.png"></p>
<p>为什么把1.7的结构改为1.8？</p>
<p>数据结构不同，提高了并发性，</p>
<p>hash碰撞，8有红黑树</p>
<p>并发安全，7segment，lock，8 cas+syn</p>
<p>复杂度，7链表n，8红黑树logn</p>
<p>为什么超过8转为红黑树：默认链表，占用内存更少，想要达到8概率很低</p>
<p>组合操作线程不安全：replace方法</p>
<h3 id="copyonwriteArrayList"><a href="#copyonwriteArrayList" class="headerlink" title="copyonwriteArrayList"></a>copyonwriteArrayList</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811140642.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811165856.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811165923.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811170040.png"></p>
<p>add方法用lock加锁</p>
<h3 id="阻塞队列"><a href="#阻塞队列" class="headerlink" title="阻塞队列"></a>阻塞队列</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811175340.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ArrayBlockingQueue</span><br><span class="line">有界，容量限制</span><br><span class="line">put方法Lock锁</span><br><span class="line">LinkedBlockingQueue</span><br><span class="line">无界，容量为最大值</span><br><span class="line">Put方法lock锁和原子类锁</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811184510.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811184752.png"></p>
<h2 id="并发流程"><a href="#并发流程" class="headerlink" title="并发流程"></a>并发流程</h2><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210811190649.png"></p>
<h3 id="countdown"><a href="#countdown" class="headerlink" title="countdown"></a>countdown</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812094029.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//多等1</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountDownTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        CountDownLatch latch = <span class="keyword">new</span> CountDownLatch(<span class="number">5</span>);</span><br><span class="line">        ExecutorService service = Executors.newFixedThreadPool(<span class="number">5</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="keyword">int</span> n = i+<span class="number">1</span>;</span><br><span class="line">            Runnable runnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">                        System.out.println(n+<span class="string">&quot;end&quot;</span>);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                        e.printStackTrace();</span><br><span class="line">                    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                        latch.countDown();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            service.submit(runnable);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;start&quot;</span>);</span><br><span class="line">        latch.await();</span><br><span class="line">        System.out.println(<span class="string">&quot;all end&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 多等1</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountDownTest2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        CountDownLatch latch = <span class="keyword">new</span> CountDownLatch(<span class="number">1</span>);</span><br><span class="line">        ExecutorService service = Executors.newFixedThreadPool(<span class="number">5</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="keyword">int</span> n = i+<span class="number">1</span>;</span><br><span class="line">            Runnable runnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    System.out.println(n+<span class="string">&quot;wait&quot;</span>);</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        latch.await();</span><br><span class="line">                        System.out.println(n+<span class="string">&quot;start&quot;</span>);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                        e.printStackTrace();</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            service.submit(runnable);</span><br><span class="line">        &#125;</span><br><span class="line">        Thread.sleep(<span class="number">500</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;can&quot;</span>);</span><br><span class="line">        latch.countDown();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//多等一和一等多结合</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountDownTest3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        CountDownLatch latch = <span class="keyword">new</span> CountDownLatch(<span class="number">1</span>);</span><br><span class="line">        CountDownLatch count = <span class="keyword">new</span> CountDownLatch(<span class="number">5</span>);</span><br><span class="line">        ExecutorService service = Executors.newFixedThreadPool(<span class="number">5</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="keyword">int</span> n = i+<span class="number">1</span>;</span><br><span class="line">            Runnable runnable = <span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    System.out.println(n+<span class="string">&quot;等待跑&quot;</span>);</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        latch.await();</span><br><span class="line">                        System.out.println(n+<span class="string">&quot;开始跑&quot;</span>);</span><br><span class="line">                        Thread.sleep(<span class="keyword">new</span> Random().nextInt(<span class="number">1000</span>));</span><br><span class="line">                        System.out.println(n+<span class="string">&quot;到终点&quot;</span>);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                        e.printStackTrace();</span><br><span class="line">                    &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">                        count.countDown();</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            service.submit(runnable);</span><br><span class="line">        &#125;</span><br><span class="line">        Thread.sleep(<span class="number">500</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;比赛开始&quot;</span>);</span><br><span class="line">        latch.countDown();</span><br><span class="line">        count.await();</span><br><span class="line">        System.out.println(<span class="string">&quot;所有都到了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="信号量"><a href="#信号量" class="headerlink" title="信号量"></a>信号量</h3><p>​    <img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812102135.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812102225.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105413.png"></p>
<h3 id="condition"><a href="#condition" class="headerlink" title="condition"></a>condition</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105540.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812105713.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Conditiontest</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> ReentrantLock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line">    Condition condition = lock.newCondition();</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">method1</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;wait&quot;</span>);</span><br><span class="line">            condition.await();</span><br><span class="line">            System.out.println(<span class="string">&quot;start&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">method2</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        lock.lock();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;complete&quot;</span>);</span><br><span class="line">            condition.signal();</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            lock.unlock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        Conditiontest r1 = <span class="keyword">new</span> Conditiontest();</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">                    r1.method2();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;).start();</span><br><span class="line">        r1.method1();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>生产消费者案例：</p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812112402.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 描述：     演示用Condition实现生产者消费者模式</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConditionDemo2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> queueSize = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">private</span> PriorityQueue&lt;Integer&gt; queue = <span class="keyword">new</span> PriorityQueue&lt;Integer&gt;(queueSize);</span><br><span class="line">    <span class="keyword">private</span> Lock lock = <span class="keyword">new</span> ReentrantLock();</span><br><span class="line">    <span class="keyword">private</span> Condition notFull = lock.newCondition();</span><br><span class="line">    <span class="keyword">private</span> Condition notEmpty = lock.newCondition();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ConditionDemo2 conditionDemo2 = <span class="keyword">new</span> ConditionDemo2();</span><br><span class="line">        Producer producer = conditionDemo2.<span class="function">new <span class="title">Producer</span><span class="params">()</span></span>;</span><br><span class="line">        Consumer consumer = conditionDemo2.n<span class="function">ew <span class="title">Consumer</span><span class="params">()</span></span>;</span><br><span class="line">        producer.start();</span><br><span class="line">        consumer.start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Consumer</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            consume();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">consume</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                lock.lock();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">while</span> (queue.size() == <span class="number">0</span>) &#123;</span><br><span class="line">                        System.out.println(<span class="string">&quot;队列空，等待数据&quot;</span>);</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            notEmpty.await();</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                            e.printStackTrace();</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    queue.poll();</span><br><span class="line">                    notFull.signalAll();</span><br><span class="line">                    System.out.println(<span class="string">&quot;从队列里取走了一个数据，队列剩余&quot;</span> + queue.size() + <span class="string">&quot;个元素&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                    lock.unlock();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Producer</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            produce();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">produce</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                lock.lock();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">while</span> (queue.size() == queueSize) &#123;</span><br><span class="line">                        System.out.println(<span class="string">&quot;队列满，等待有空余&quot;</span>);</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            notFull.await();</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                            e.printStackTrace();</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    queue.offer(<span class="number">1</span>);</span><br><span class="line">                    notEmpty.signalAll();</span><br><span class="line">                    System.out.println(<span class="string">&quot;向队列插入了一个元素，队列剩余空间&quot;</span> + (queueSize - queue.size()));</span><br><span class="line">                &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                    lock.unlock();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="循环栅栏"><a href="#循环栅栏" class="headerlink" title="循环栅栏"></a>循环栅栏</h3><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812121712.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812123022.png"></p>
<h2 id="AQS"><a href="#AQS" class="headerlink" title="AQS"></a>AQS</h2><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812132944.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133102.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133221.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133331.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133506.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812133546.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812135215.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812191545.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812192216.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812193225.png"></p>
<h2 id="future-callable"><a href="#future-callable" class="headerlink" title="future callable"></a>future callable</h2><p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812194037.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812194948.png"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812212311.png"></p>
<p><img src="C:\Users\sunlei\AppData\Roaming\Typora\typora-user-images\image-20210812212233711.png" alt="image-20210812212233711"></p>
<p><img src="https://gitee.com/heart_perturbation/sxblogimg/raw/master/img/20210812212535.png"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/08/21/java%E5%B9%B6%E5%8F%91%E8%BF%9B%E9%98%B6%E7%AC%94%E8%AE%B0/" data-id="ckslhuewq00001gtj8kivd3w5" data-title="" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/08/21/java%E5%B9%B6%E5%8F%91%E7%AC%94%E8%AE%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2021/08/21/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/08/21/java%E5%B9%B6%E5%8F%91%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/08/21/java%E5%B9%B6%E5%8F%91%E8%BF%9B%E9%98%B6%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/08/21/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>